package com.burger.counter.business;

import com.burger.counter.beans.User;
import com.burger.counter.dao.UserDao;
import com.sun.corba.se.impl.legacy.connection.USLPort;


public class UserBusiness {
	public void insertUserDetails(String firstName, String lastName, String eMail, int zipcode, 
			String dateOfBirth, int regionID, String password, String isAdminOrCustomer) throws Exception{
		try
		{
			System.out.println("we r in business layer");
			UserDao userDao = new UserDao(); 
			String strYear,strMonth,strDate,updatedDOB;
			System.out.println("DOB :" + dateOfBirth);
			
			dateOfBirth = dateOfBirth.trim();
			String[] newdateOfBirth = dateOfBirth.split("-");
			if(newdateOfBirth != null)
			{
				strYear = newdateOfBirth[2];
				System.out.println(strYear);
				strMonth =newdateOfBirth[0];
				System.out.println(strMonth);
				strDate = newdateOfBirth[1];
				System.out.println(strDate);
				updatedDOB = strYear + "-" + strMonth +"-" + strDate;
				System.out.println(updatedDOB);
				userDao.insertUserDetails(firstName, lastName, 
						eMail, zipcode, updatedDOB, regionID, password, 
						isAdminOrCustomer);
			}
			else
			{
				System.err.println("Date of Birth Not in Proper format!! ");
			}
			System.out.println("we r in business layer again");
		}
		catch(Exception e)
		{
			throw e;
		}
	}
	public void updateUserData(String firstName, String lastName, String eMail, int zipcode, 
			String dateOfBirth, int regionID, String password, String isAdminOrCustomer,Integer userId) throws Exception{
		try
		{
			System.out.println("we r in business layer");
			UserDao userDao = new UserDao(); 
			String strYear,strMonth,strDate,updatedDOB;
			
			dateOfBirth = dateOfBirth.trim();
			String[] newdateOfBirth = dateOfBirth.split("-");
			if(newdateOfBirth != null)
			{
				strYear = newdateOfBirth[2];
				System.out.println(strYear);
				strMonth =newdateOfBirth[0];
				System.out.println(strMonth);
				strDate = newdateOfBirth[1];
				System.out.println(strDate);
				updatedDOB = strYear + "-" + strMonth +"-" + strDate;
				System.out.println(updatedDOB);
				userDao.updateUserData(firstName, lastName, eMail, zipcode,
						updatedDOB, regionID, password, isAdminOrCustomer, userId);
			}
			else
			{
				System.err.println("Date of Birth Not in Proper format!! ");
			}
			
			System.out.println("we r in business layer again");
		}
		catch(Exception e)
		{
			throw e;
		}
	}

		public int checkPassword(String eMail, String password){
			System.out.println("we r in business layer checkPassword");
			UserDao userDao = new UserDao(); 
			User[] u = userDao.getUserDetails(eMail);
			
			System.out.println("Pasword from DB"+u[0].getPassword());
			if(password.equals(u[0].getPassword())){
				return u[0].getRegionID();
			}
			else{
				return 1;
			}
		}
		public int getUserId(String eMail){
			UserDao userDao = new UserDao(); 
			User[] u = userDao.getUserDetails(eMail);
			if(u != null){
				return u[0].getUserId();
			}else{
				return 1;
			}
		}
		public User[] getUserDetails(String email){
			System.out.println("In Bussiness Layer");
			UserDao userDAO = new UserDao();
			User[] userDetails = userDAO.getUserDetails(email);
			
			return userDetails;
		}
}
